<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Setting extends Admin_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		$this->load->model('Setting_model', 'setModel');
	}

	public function group()
	{
		$data=$this->data;
		$this->load->view('admin/Setting/group',$data);
	}
	//页面渲染
	public function group_list()
	{
		$name=$this->input->get('name');
		$where=[];
		if(!empty($name))
		{
			$where+=['name'=>$name];
		}
		$key=$this->input->get('key');
		if(!empty($key))
		{
			$where+=['key'=>$key];
		}
		$limit = $this->input->get('limit');
		$page = $this->input->get('page')==1?0:($this->input->get('page')-1)*$limit;
		$list=$this->setModel->getGroupList($limit,$page,$where);
		$result['code'] = 0;
		$result['msg'] = '';
		$result['data'] = $list;
		$result['count'] = $this->setModel->getGroupCount();
		echo json_encode($result);
	}
	public function group_add()
	{
		if($this->input->is_ajax_request())
		{
			$key=$this->input->post('data')['key'];
			$name=$this->input->post('data')['name'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($key&&$name&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->setModel->insertGroup(['key'=>$key,'name'=>$name,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'添加成功']));
			else die(json_encode(['code'=>0,'content'=>'添加失败']));
		}
		else
		{
			$this->load->view('admin/Setting/group_add');
		}
	}
	public function group_edit()
	{
		if($this->input->is_ajax_request())
		{
			$key=$this->input->post('data')['key'];
			$name=$this->input->post('data')['name'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($key&&$name&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->setModel->updateGroup(intval($this->input->post('data')['id']),['key'=>$key,'name'=>$name,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'编辑成功']));
			else die(json_encode(['code'=>0,'content'=>'编辑失败']));
		}
		else
		{
			$data=$this->setModel->getGroup($this->input->get('id'));
			$this->load->view('admin/Setting/group_edit',$data);
		}
	}
	public function group_del()
	{
		$id=intval($this->input->post('id'));
		$back=$this->setModel->testGroup($id);
		if($back==2)
		{
			$res=$this->setModel->delGroup(intval($this->input->post('id')));
			if($res) die(json_encode(['code'=>1,'content'=>'删除成功']));
			else die(json_encode(['code'=>0,'content'=>'删除失败']));
		}
		else die(json_encode(['code'=>0,'content'=>'请先移除此分类下的配置项']));
	}

	public function set()
	{
		$data=$this->data;
		$this->load->view('admin/Setting/set',$data);
	}
	//页面渲染
	public function set_list()
	{
		$group_key=$this->input->get('group_key');
		$where=[];
		if(!empty($group_key))
		{
			$where+=['group_key'=>$group_key];
		}
		$key=$this->input->get('key');
		if(!empty($key))
		{
			$where+=['key'=>$key];
		}
		$limit = $this->input->get('limit');
		$page = $this->input->get('page')==1?0:($this->input->get('page')-1)*$limit;
		$list=$this->setModel->getSetList($limit,$page,$where);
		$result['code'] = 0;
		$result['msg'] = '';
		$result['data'] = $list;
		$result['count'] = $this->setModel->getSetCount();
		echo json_encode($result);
	}
	public function set_add()
	{
		if($this->input->is_ajax_request())
		{
			$group_key=$this->input->post('data')['group_key'];
			$key=$this->input->post('data')['key'];
			$name=$this->input->post('data')['name'];
			$value=$this->input->post('data')['value'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($group_key&&$key&&$name&&$value&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->setModel->insertSet(['group_key'=>$group_key,'key'=>$key,'name'=>$name,'value'=>$value,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'添加成功']));
			else die(json_encode(['code'=>0,'content'=>'添加失败']));
		}
		else
		{
			$data['key']=$this->setModel->getGroupKey();
			$this->load->view('admin/Setting/set_add',$data);
		}
	}
	public function set_edit()
	{
		if($this->input->is_ajax_request())
		{
			$group_key=$this->input->post('data')['group_key'];
			$key=$this->input->post('data')['key'];
			$name=$this->input->post('data')['name'];
			$value=$this->input->post('data')['value'];
			$inx=intval($this->input->post('data')['inx']);
			if(!($group_key&&$key&&$name&&$value&&$inx))
			{
				die(json_encode(['code'=>0,'content'=>'缺少参数']));
			}
			$res=$this->setModel->updateSet(intval($this->input->post('data')['id']),['group_key'=>$group_key,'key'=>$key,'name'=>$name,'value'=>$value,'inx'=>$inx]);
			if($res) die(json_encode(['code'=>1,'content'=>'编辑成功']));
			else die(json_encode(['code'=>0,'content'=>'编辑失败']));
		}
		else
		{
			$data['key']=$this->setModel->getGroupKey();
			$data['data']=$this->setModel->getSet($this->input->get('id'));
			$this->load->view('admin/Setting/set_edit',$data);
		}
	}
	public function set_del()
	{
		$id=intval($this->input->post('id'));
		$res=$this->setModel->delSet(intval($this->input->post('id')));
		if($res) die(json_encode(['code'=>1,'content'=>'删除成功']));
		else die(json_encode(['code'=>0,'content'=>'删除失败']));
	}
}
